import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import reducer from './reducer'
// 引入配合中间件使用的调试工具相关包
import { composeWithDevTools } from 'redux-devtools-extension'

const store = createStore(
  reducer,
  { c: 100, },
  composeWithDevTools(applyMiddleware(thunk))
)
store.subscribe(() => {
  console.log(store.getState())
})

// 实现异步操作:

// 1. 下载redux-thunk
// 2. 引入applyMiddleWare, 在createStore中调用
// 3. 引入redux-thunk,传入applyMiddleWare
// 4. 调用dispatch
store.dispatch((dispatch) => {
  setTimeout(() => {
    dispatch({ type: 'inc' })
  }, 2000)
})

export default store
